package licode.unisop.auth.domain.gateway;

/**
 * 密码复杂度校验逻辑
 *
 * @author WR
 * @version 1.0
 * @date 2024-06-20 14:43
 */
public class PasswordValidator {
    private static final int MIN_PASSWORD_LENGTH = 8;
    private static final String SPECIAL_CHARACTER_LIST = "!@#$%^&*()-+~";

    public static boolean validate(final String password) {
        if (password.length() < MIN_PASSWORD_LENGTH) {
            return false;
        }

        boolean hasUppercase = !password.equals(password.toLowerCase());
        boolean hasLowercase = !password.equals(password.toUpperCase());
        boolean hasSpecial = containsSpecialCharacter(password);
        boolean hasDigits = password.matches(".*\\d.*");

        return hasUppercase && hasLowercase && hasSpecial && hasDigits;
    }

    private static boolean containsSpecialCharacter(String input) {
        for (int i = 0; i < input.length(); i++) {
            if (SPECIAL_CHARACTER_LIST.contains(String.valueOf(input.charAt(i)))) {
                return true;
            }
        }
        return false;
    }

}
